package com.inei.reportedeverificacion.view;

import java.util.ArrayList;
import java.util.List;
import com.inei.reportedeverificacion.R;
import com.inei.reportedeverificacion.controller.ReporteCap2Controller;
import com.inei.reportedeverificacion.controller.ReporteController;
import com.inei.reportedeverificacion.model.entity.Reporte;
import com.inei.reportedeverificacion.model.entity.ReporteCap2;
import com.inei.reportedeverificacion.model.entity.ReporteCap2Tmp;
import com.inei.reportedeverificacion.model.entity.ReporteTmp;
import com.inei.reportedeverificacion.tools.HolderCap2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_Seccion_1 extends Activity {

	// componentes
	private TextView tvC1_1, tvC1_2, tvC1_18, tvC3_1, tvC5_59, tvC5_67_1,
			tvC5_67_2, tvC5_67_3, tvC5_67_4, tvC5_67_5, tvC5_67_6, tvC5_67_7,
			tvC5_67_8, tvC5_67_9, tvC5_67_10, tvC5_67_11, tvC5_69, tvC5_70;
	private Spinner spC1_1, spC1_2, spC1_18, spC3_1, spC5_59, spC5_67_1,
			spC5_67_2, spC5_67_3, spC5_67_4, spC5_67_5, spC5_67_6, spC5_67_7,
			spC5_67_8, spC5_67_9, spC5_67_10, spC5_67_11, spC5_69, spC5_70;
	private LinearLayout llC2_1;
	public List<Spinner> spinnerCap2;

	// variables
	private ReporteController con;
	private Reporte rep;
	private ReporteTmp repTmp;
	private ReporteCap2Controller con2;
	private List<ReporteCap2> cap2;
	private List<ReporteCap2Tmp> cap2Tmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_seccion_1);

		con = new ReporteController(getApplicationContext());
		con2 = new ReporteCap2Controller(getApplicationContext());

		IniciarComponentes();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_frag1, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.mnGuardar:
			GuardarDatos();
			break;
		case R.id.mnSalir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_vivienda = new Intent(this, Vivienda_Activity.class);
		ir_vivienda.putExtras(getIntent().getExtras());
		startActivity(ir_vivienda);
		finish();

		super.onBackPressed();
	}

	private void IniciarComponentes() {
		tvC1_1 = (TextView) findViewById(R.id.tvC1_1);
		tvC1_2 = (TextView) findViewById(R.id.tvC1_2);
		tvC1_18 = (TextView) findViewById(R.id.tvC1_18);
		tvC3_1 = (TextView) findViewById(R.id.tvC3_1);
		tvC5_59 = (TextView) findViewById(R.id.tvC5_59);
		tvC5_67_1 = (TextView) findViewById(R.id.tvC5_67_1);
		tvC5_67_2 = (TextView) findViewById(R.id.tvC5_67_2);
		tvC5_67_3 = (TextView) findViewById(R.id.tvC5_67_3);
		tvC5_67_4 = (TextView) findViewById(R.id.tvC5_67_4);
		tvC5_67_5 = (TextView) findViewById(R.id.tvC5_67_5);
		tvC5_67_6 = (TextView) findViewById(R.id.tvC5_67_6);
		tvC5_67_7 = (TextView) findViewById(R.id.tvC5_67_7);
		tvC5_67_8 = (TextView) findViewById(R.id.tvC5_67_8);
		tvC5_67_9 = (TextView) findViewById(R.id.tvC5_67_9);
		tvC5_67_10 = (TextView) findViewById(R.id.tvC5_67_10);
		tvC5_67_11 = (TextView) findViewById(R.id.tvC5_67_11);
		tvC5_69 = (TextView) findViewById(R.id.tvC5_69);
		tvC5_70 = (TextView) findViewById(R.id.tvC5_70);

		spC1_1 = (Spinner) findViewById(R.id.spC1_1);
		spC1_2 = (Spinner) findViewById(R.id.spC1_2);
		spC1_18 = (Spinner) findViewById(R.id.spC1_18);
		spC3_1 = (Spinner) findViewById(R.id.spC3_1);
		spC5_59 = (Spinner) findViewById(R.id.spC5_59);
		spC5_67_1 = (Spinner) findViewById(R.id.spC5_67_1);
		spC5_67_2 = (Spinner) findViewById(R.id.spC5_67_2);
		spC5_67_3 = (Spinner) findViewById(R.id.spC5_67_3);
		spC5_67_4 = (Spinner) findViewById(R.id.spC5_67_4);
		spC5_67_5 = (Spinner) findViewById(R.id.spC5_67_5);
		spC5_67_6 = (Spinner) findViewById(R.id.spC5_67_6);
		spC5_67_7 = (Spinner) findViewById(R.id.spC5_67_7);
		spC5_67_8 = (Spinner) findViewById(R.id.spC5_67_8);
		spC5_67_9 = (Spinner) findViewById(R.id.spC5_67_9);
		spC5_67_10 = (Spinner) findViewById(R.id.spC5_67_10);
		spC5_67_11 = (Spinner) findViewById(R.id.spC5_67_11);
		spC5_69 = (Spinner) findViewById(R.id.spC5_69);
		spC5_70 = (Spinner) findViewById(R.id.spC5_70);

		llC2_1 = (LinearLayout) findViewById(R.id.llC2_1);
	}

	private void CargarDatos() {
		repTmp = new ReporteTmp();
		repTmp.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));
		repTmp = con.ReporteTmpBuscar(repTmp);

		if (repTmp != null) {
			if (repTmp.getVIVREM() != null) {
				tvC1_1.setText(repTmp.getVIVREM());
			}
			if (repTmp.getC1P15_RFINAL() != 0) {
				tvC1_2.setText(tabla01(repTmp.getC1P15_RFINAL()));
			}
			if (repTmp.getC1P18() != 0) {
				tvC1_18.setText(rptaSiNo(repTmp.getC1P18()));
			}
			if (repTmp.getT_05_DIG_CAP03() != 0) {
				tvC3_1.setText(repTmp.getT_05_DIG_CAP03() + "");
			}
			if (repTmp.getC1P59() != 0 || repTmp.getC1P59_O() != null
					|| repTmp.getC1P59_M() != null) {
				tvC5_59.setText(tabla03(repTmp.getC1P59()) + " - "
						+ repTmp.getC1P59_O() + " - " + repTmp.getC1P59_M());
			}
			if (repTmp.getC1P67_1() != 0) {
				tvC5_67_1.setText(rptaSiNo(repTmp.getC1P67_1()));
			}
			if (repTmp.getC1P67_2() != 0) {
				tvC5_67_2.setText(rptaSiNo(repTmp.getC1P67_2()));
			}
			if (repTmp.getC1P67_3() != 0) {
				tvC5_67_3.setText(rptaSiNo(repTmp.getC1P67_3()));
			}
			if (repTmp.getC1P67_4() != 0) {
				tvC5_67_4.setText(rptaSiNo(repTmp.getC1P67_4()));
			}
			if (repTmp.getC1P67_5() != 0) {
				tvC5_67_5.setText(rptaSiNo(repTmp.getC1P67_5()));
			}
			if (repTmp.getC1P67_6() != 0) {
				tvC5_67_6.setText(rptaSiNo(repTmp.getC1P67_6()));
			}
			if (repTmp.getC1P67_7() != 0) {
				tvC5_67_7.setText(rptaSiNo(repTmp.getC1P67_7()));
			}
			if (repTmp.getC1P67_8() != 0) {
				tvC5_67_8.setText(rptaSiNo(repTmp.getC1P67_8()));
			}
			if (repTmp.getC1P67_9() != 0) {
				tvC5_67_9.setText(rptaSiNo(repTmp.getC1P67_9()));
			}
			if (repTmp.getC1P67_10() != 0) {
				tvC5_67_10.setText(rptaSiNo(repTmp.getC1P67_10()));
			}
			if (repTmp.getC1P67_11() != 0) {
				tvC5_67_11.setText(rptaSiNo(repTmp.getC1P67_11()));
			}
			if (repTmp.getC1P69() != 0) {
				tvC5_69.setText(rptaSiNo(repTmp.getC1P69()));
			}
			if (repTmp.getC1P70() != 0) {
				tvC5_70.setText(rptaSiNo(repTmp.getC1P70()));
			}
		}

		rep = new Reporte();
		rep.setCLAVEVIVHOGAR(getIntent().getExtras().getString("CLAVEVIVHOGAR"));
		rep = con.ReporteBuscar(rep);

		if (rep != null) {
			if (rep.getVIVREM() != 0) {
				spC1_1.setSelection(rep.getVIVREM());
			}
			if (rep.getC1P15_RFINAL() != 0) {
				spC1_2.setSelection(rep.getC1P15_RFINAL());
			}
			if (rep.getC1P18() != 0) {
				spC1_18.setSelection(rep.getC1P18());
			}
			if (rep.getT_05_DIG_CAP03_COUNT() != 0) {
				spC3_1.setSelection(rep.getT_05_DIG_CAP03_COUNT());
			}
			if (rep.getC1P59() != 0) {
				spC5_59.setSelection(rep.getC1P59());
			}
			if (rep.getC1P67_1() != 0) {
				spC5_67_1.setSelection(rep.getC1P67_1());
			}
			if (rep.getC1P67_2() != 0) {
				spC5_67_2.setSelection(rep.getC1P67_2());
			}
			if (rep.getC1P67_3() != 0) {
				spC5_67_3.setSelection(rep.getC1P67_3());
			}
			if (rep.getC1P67_4() != 0) {
				spC5_67_4.setSelection(rep.getC1P67_4());
			}
			if (rep.getC1P67_5() != 0) {
				spC5_67_5.setSelection(rep.getC1P67_5());
			}
			if (rep.getC1P67_6() != 0) {
				spC5_67_6.setSelection(rep.getC1P67_6());
			}
			if (rep.getC1P67_7() != 0) {
				spC5_67_7.setSelection(rep.getC1P67_7());
			}
			if (rep.getC1P67_8() != 0) {
				spC5_67_8.setSelection(rep.getC1P67_8());
			}
			if (rep.getC1P67_9() != 0) {
				spC5_67_9.setSelection(rep.getC1P67_9());
			}
			if (rep.getC1P67_10() != 0) {
				spC5_67_10.setSelection(rep.getC1P67_10());
			}
			if (rep.getC1P67_11() != 0) {
				spC5_67_11.setSelection(rep.getC1P67_11());
			}
			if (rep.getC1P69() != 0) {
				spC5_69.setSelection(rep.getC1P69());
			}
			if (rep.getC1P70() != 0) {
				spC5_70.setSelection(rep.getC1P70());
			}
		}

		cap2Tmp = con2.ReporteCap2TmpLeerXHogar(getIntent().getExtras()
				.getString("CLAVEVIVHOGAR"));

		cap2 = con2.ReporteCap2LeerXHogar(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));

		if (cap2Tmp != null) {
			CargarCap2();
		}
	}

	private void CargarCap2() {
		spinnerCap2 = new ArrayList<Spinner>();
		if (llC2_1 != null) {
			llC2_1.removeAllViews();
		}
		for (int i = 0; i < cap2Tmp.size(); i++) {
			View item = null;
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.cap2_item, null);
			ReporteCap2Tmp obj = cap2Tmp.get(i);
			HolderCap2 holder = new HolderCap2();
			holder.tvcap21 = (TextView) item.findViewById(R.id.tvcap21);
			holder.tvcap22 = (TextView) item.findViewById(R.id.tvcap22);
			holder.tvcap23 = (TextView) item.findViewById(R.id.tvcap23);
			holder.tvcap24 = (TextView) item.findViewById(R.id.tvcap24);
			holder.tvcap25 = (TextView) item.findViewById(R.id.tvcap25);
			holder.spcap26 = (Spinner) item.findViewById(R.id.spcap26);

			holder.tvcap21.setText((obj.getC1P21A() + " " + obj.getC1P21B()
					+ " " + obj.getC1P21C()));
			holder.tvcap22.setText(tabla02(obj.getC1P22()) + "");
			holder.tvcap23.setText(obj.getC1P26_ANIO() + "/"
					+ obj.getC1P26_MESES());
			holder.tvcap24.setText(rptaSiNo(obj.getC1P24()) + "");
			holder.tvcap25.setText(obj.getC1P27_DIA() + "/"
					+ obj.getC1P27_MES() + "/" + obj.getC1P27_ANIO());

			spinnerCap2.add(holder.spcap26);
			llC2_1.addView(item);
		}

		if (cap2 != null) {
			for (int i = 0; i < spinnerCap2.size(); i++) {
				spinnerCap2.get(i).setSelection(cap2.get(i).getCodigo());
			}
		}
	}

	public void GuardarDatos() {
		if (rep == null) {
			rep = new Reporte();
		}

		rep.setVIVREM(spC1_1.getSelectedItemPosition());
		rep.setC1P15_RFINAL(spC1_2.getSelectedItemPosition());
		rep.setC1P18(spC1_18.getSelectedItemPosition());
		rep.setT_05_DIG_CAP03_COUNT(spC3_1.getSelectedItemPosition());
		rep.setC1P59(spC5_59.getSelectedItemPosition());
		rep.setC1P67_1(spC5_67_1.getSelectedItemPosition());
		rep.setC1P67_2(spC5_67_2.getSelectedItemPosition());
		rep.setC1P67_3(spC5_67_3.getSelectedItemPosition());
		rep.setC1P67_4(spC5_67_4.getSelectedItemPosition());
		rep.setC1P67_5(spC5_67_5.getSelectedItemPosition());
		rep.setC1P67_6(spC5_67_6.getSelectedItemPosition());
		rep.setC1P67_7(spC5_67_7.getSelectedItemPosition());
		rep.setC1P67_8(spC5_67_8.getSelectedItemPosition());
		rep.setC1P67_9(spC5_67_9.getSelectedItemPosition());
		rep.setC1P67_10(spC5_67_10.getSelectedItemPosition());
		rep.setC1P67_11(spC5_67_11.getSelectedItemPosition());
		rep.setC1P69(spC5_69.getSelectedItemPosition());
		rep.setC1P70(spC5_70.getSelectedItemPosition());

		if (rep.getCLAVEVIVHOGAR() == null) {
			rep.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
					"CLAVEVIVHOGAR"));
			con.ReporteInsertar(rep);
		} else {
			con.ReporteModificar(rep);
		}

		int n = 0;
		if (cap2 == null) {
			cap2 = new ArrayList<ReporteCap2>();
			for (int i = 0; i < cap2Tmp.size(); i++) {
				ReporteCap2 r = new ReporteCap2();
				r.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
						"CLAVEVIVHOGAR"));
				r.setNRO_ORDEN(cap2Tmp.get(i).getNRO_ORDEN());
				cap2.add(r);
			}
			n = 1;
		}
		for (int i = 0; i < cap2Tmp.size(); i++) {
			cap2.get(i).setCodigo(spinnerCap2.get(i).getSelectedItemPosition());
			if (n == 1) {
				if (con2.ReporteCap2Insertar(cap2.get(i)) > 0) {
					Intent ir_frag2 = new Intent(this, Fragment_Seccion_2.class);
					Bundle datos = getIntent().getExtras();
					ir_frag2.putExtras(datos);
					startActivity(ir_frag2);

					Toast.makeText(this, "Se guardo con exito",
							Toast.LENGTH_SHORT).show();

					finish();
				} else {
					Toast.makeText(this, "Error al guardar", Toast.LENGTH_SHORT)
							.show();
				}
			} else {
				if (con2.ReporteCap2Modificar(cap2.get(i)) > 0) {
					Intent ir_frag2 = new Intent(this, Fragment_Seccion_2.class);
					Bundle datos = getIntent().getExtras();
					ir_frag2.putExtras(datos);
					startActivity(ir_frag2);

					Toast.makeText(this, "Se actualizo con exito",
							Toast.LENGTH_SHORT).show();

					finish();
				} else {
					Toast.makeText(this, "Error al actualizar",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	private String tabla01(int pos) {
		return getResources().getStringArray(R.array.tabla01)[pos];
	}

	private String tabla02(int pos) {
		return getResources().getStringArray(R.array.tabla02)[pos];
	}

	private String tabla03(int pos) {
		return getResources().getStringArray(R.array.tabla03)[pos];
	}

	private String rptaSiNo(int pos) {
		return getResources().getStringArray(R.array.rptaSiNo)[pos];
	}
}
